<template>
    <div>
        <!-- 搜索组件 -->
        <search-form :items="formItems" :params="params" :on-submit="onSearch" :btn-loading="loading" />

        <!-- 表格区域 -->
        <div class="table-body">
            <!-- 左上角按钮 -->
            <div class="table-right-top">
                <el-button v-auth="'add'" type="success" icon="el-icon-plus" @click="onAdd">{{ $i18n.t('CREATE_APPROVE_ACCOUNT') }}</el-button>
            </div>

            <!-- 表格 -->
            <el-table border style="width: 100%;" :data="data" v-loading="loading">
                <el-table-column align="center" prop="nickname" :label="$i18n.t('NAME')" fixed="left" :min-width="150"></el-table-column>
                <el-table-column align="center" prop="username" :label="$i18n.t('USERNAME')" :min-width="150"></el-table-column>
                <el-table-column align="center" prop="phone" :label="$i18n.t('PHONE_NUMBER')" :min-width="150"></el-table-column>
                <el-table-column align="center" prop="email" :label="$i18n.t('EMAIL')" :min-width="150"></el-table-column>
                <el-table-column align="center" prop="is_auto_task" :label="$i18n.t('IS_AUTO_TASK')" :min-width="130">
                    <template slot-scope="scope">
                        <el-tag size="mini" :type="scope.row.is_auto_task ? 'success' : 'danger'" effect="dark">
                            {{ scope.row.is_auto_task ? $i18n.t('YES') : $i18n.t('NO') }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="is_tel" :label="$i18n.t('IS_TEL')" :min-width="130">
                    <template slot-scope="scope">
                        <el-tag size="mini" :type="scope.row.is_tel ? 'success' : 'danger'" effect="dark">
                            {{ scope.row.is_tel ? $i18n.t('YES') : $i18n.t('NO') }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="tel_account" :label="$i18n.t('TEL_ACCOUNT')" :min-width="130"></el-table-column>
                <el-table-column align="center" prop="is_receive_task" :label="$i18n.t('IS_RECEIVE_TASK')" :min-width="130">
                    <template slot-scope="scope">
                        <el-tag size="mini" :type="scope.row.is_receive_task ? 'success' : 'danger'" effect="dark">
                            {{ scope.row.is_receive_task ? $i18n.t('YES') : $i18n.t('NO') }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="daily_task_limit" :label="$i18n.t('DAILY_TASK_LIMIT')" :min-width="100"></el-table-column>
                <el-table-column align="center" prop="status" :label="$i18n.t('STATUS')" :min-width="100">
                    <template slot-scope="scope">
                        <el-tag size="mini" v-if="scope.row.status === 1" type="success" effect="dark">
                            {{ $i18n.t('NORMAL') }}
                        </el-tag>
                        <el-tag size="mini" v-if="scope.row.status === 2" type="danger" effect="dark">
                            {{ $i18n.t('DISABLED') }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="created_at" :label="$i18n.t('CREATE_TIME')" :min-width="150"></el-table-column>
                <el-table-column align="center" prop="updated_at" :label="$i18n.t('UPDATE_TIME')" :min-width="150"></el-table-column>
                <!-- 右侧按钮 -->
                <el-table-column align="center" :label="$i18n.t('ACTION')" fixed="right" :width="150">
                    <template slot-scope="scope">
                        <span v-auth="'edit'">
                            <el-link v-auth="'edit'" icon="el-icon-edit" @click="onEdit(scope.row)">{{ $i18n.t('EDIT') }}</el-link>
                            <el-divider direction="vertical"></el-divider>
                        </span>
                        <!-- 删除 -->
                        <poptip
                            confirm
                            :title="$i18n.t('CONFIRM_DELETE_TIP')"
                            @on-ok="onDelete(scope.row)"
                            :ok-text="$i18n.t('CONFIRM')"
                            :cancel-text="$i18n.t('CANCEL')"
                        >
                            <el-link v-auth="'delete'" icon="el-icon-delete">{{ $i18n.t('DELETE') }}</el-link>
                        </poptip>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <div class="table-pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next, ->, sizes"
                    :page-size.sync="page.perPage"
                    :total="page.total"
                    :page-count="page.pageCount"
                    :current-page.sync="page.currentPage"
                    :page-sizes="PAGES_SIZE"
                    @current-change="onCurrentPageChange"
                    @size-change="onPerPageChange"
                >
                </el-pagination>
            </div>
        </div>
        <el-drawer
            :title="$i18n.t(mdl.id > 0 ? 'EDIT' : 'ADD')"
            :visible.sync="drawer"
            destroy-on-close
            ref="drawer"
            size="400px"
        >
            <account-form-modal :mdl="mdl" :handle-close-drawer="handleCloseDrawer"></account-form-modal>
        </el-drawer>
    </div>
</template>

<script>
    import SearchForm from '@/components/SearchForm'
    import { getApproveAccount, delApproveAccount } from '@/api/approve'
    import { PAGES_SIZE } from '@/config/constants'
    import AccountFormModal from '@/views/approve/components/AccountFormModal'

    export default {
        name: 'ApproveAccountList',
        components: { SearchForm, AccountFormModal },
        data () {
            return {
                PAGES_SIZE,
                params: {
                },
                data: [],
                page: {
                    total: 0,
                    currentPage: 1,
                    perPage: PAGES_SIZE[0],
                    pageCount: 1
                },
                loading: false,
                mdl: {},
                drawer: false
            }
        },
        created () {
            this.fetch()
        },
        computed: {
            formItems () {
                // 用户状态筛选
                const statusOptions = [
                    { label: this.$i18n.t('NORMAL'), value: 1 },
                    { label: this.$i18n.t('DISABLED'), value: 2 }
                ]
                // 构建搜索表单
                return [
                    { title: this.$i18n.t('USERNAME'), type: 'input', key: 'username', maxlength: 50 },
                    { title: this.$i18n.t('NICKNAME'), type: 'input', key: 'nickname', maxlength: 20 },
                    { title: this.$i18n.t('PHONE_NUMBER'), type: 'input', key: 'phone', maxlength: 20 },
                    { title: this.$i18n.t('EMAIL'), type: 'input', key: 'email', maxlength: 20 },
                    { title: this.$i18n.t('STATUS'), type: 'select', key: 'status', options: statusOptions }
                ]
            }
        },
        methods: {
            // 搜索按钮
            onSearch () {
                this.fetch()
            },
            // 从接口拉取数据
            fetch () {
                this.loading = true
                getApproveAccount({ ...this.params, page: this.page.currentPage, perPage: this.page.perPage }).then((res) => {
                    // eslint-disable-next-line camelcase
                    const { data, last_page, total } = res.result
                    this.data = data
                    this.page.total = total
                    // eslint-disable-next-line camelcase
                    this.page.pageCount = last_page
                }).finally(() => {
                    this.loading = false
                })
            },
            // 页数切换
            onCurrentPageChange (page) {
                this.page.currentPage = page
                this.fetch()
            },
            // 每页显示数量修改
            onPerPageChange (perPage) {
                this.page.perPage = perPage
                this.fetch()
            },
            onEdit (row) {
                this.mdl = Object.assign({}, row)
                this.drawer = true
            },
            onAdd () {
                this.mdl = Object.assign({}, {
                    id: 0,
                    username: '',
                    password: '',
                    nickname: '',
                    phone: '',
                    email: '',
                    status: 1,
                    is_auto_task: true,
                    is_tel: false,
                    daily_task_limit: 0
                })
                this.drawer = true
            },
            onDelete (row) {
                delApproveAccount(row.id).then(res => {
                    this.$Message.success(this.$i18n.t('HANDLE_SUCCESS'))
                    this.fetch()
                    // 刷新选项列表
                    this.$store.dispatch('GetOptions').then(() => {})
                })
            },
            handleCloseDrawer () {
                this.$refs.drawer.closeDrawer()
            }
        }
    }
</script>

<style lang="less" scoped>
</style>
